package org.giovanolli.mumi.controller.business;

import java.util.Date;

import org.giovanolli.mumi.controller.AbstractController;
import org.giovanolli.mumi.controller.AbstractModel;
import org.giovanolli.mumi.controller.UseCaseMode;
import org.giovanolli.mumi.entities.GeneralObservation;
import org.giovanolli.mumi.entities.Patient;
import org.giovanolli.mumi.entities.PatientLog;
import org.giovanolli.mumi.entities.filter.PatientFilter;
import org.giovanolli.mumi.entities.filter.PatientLogFilter;
import org.giovanolli.mumi.entities.filter.SpecialistFilter;
import org.giovanolli.mumi.exception.NotImplementedMethodException;
import org.giovanolli.mumi.service.PatientLogService;
import org.giovanolli.mumi.service.PatientService;
import org.giovanolli.mumi.service.Service;
import org.giovanolli.mumi.service.SpecialistService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

/**
 * The Class PatientLogController.
 * 
 * @author root
 */
@Controller("patientLogController")
public class PatientLogController extends AbstractController<PatientLog> {

	/** The service. */
	@Autowired
	private PatientLogService service;

	/** The patient service. */
	@Autowired
	private PatientService patientService;

	/** The specialist service. */
	@Autowired
	private SpecialistService specialistService;

	/** The model. */
	@Autowired
	private PatientLogModel model;

	/**
	 * Observation Events controller.
	 */
	public void addObservationEvent() {
		model.setEditedObservation(new GeneralObservation());
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.giovanolli.mumi.controller.AbstractController#bindEntityToModel()
	 */
	@Override
	protected void bindEntityToModel() {
		PatientLog editedPatientLog = model.getEditedEntity();
		model.setDate(editedPatientLog.getDate());
		model.setPatient(editedPatientLog.getPatient());
		model.setSpecialist(editedPatientLog.getSpecialist());
		model.setLog(editedPatientLog.getLog());
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.giovanolli.mumi.controller.AbstractController#bindModelToEntity()
	 */
	@Override
	protected void bindModelToEntity() {
		PatientLog editedPatientLog = model.getEditedEntity();
		editedPatientLog.setDate(model.getDate());
		editedPatientLog.setPatient(model.getPatient());
		editedPatientLog.setSpecialist(model.getSpecialist());
		editedPatientLog.setLog(model.getLog());
	}

	/**
	 * Confirm save observation event.
	 */
	public void confirmSaveObservationEvent() {
		GeneralObservation editedObservation = model.getEditedObservation();
		editedObservation.setDate(new Date());
		model.getEditedEntity().getObservations().add(editedObservation);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.giovanolli.mumi.controller.AbstractController#getEntityClass()
	 */
	protected Class<PatientLog> getEntityClass() {
		return PatientLog.class;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.giovanolli.mumi.controller.AbstractController#getExportedFileName()
	 */
	@Override
	public String getExportedFileName() throws NotImplementedMethodException {
		String patientName = getModel().getEditedEntity().getPatient().getFullName();
		patientName = patientName.replace(",", "");
		patientName = patientName.replace(" ", "_");
		return "seguimiento_" + patientName;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.giovanolli.mumi.controller.AbstractController#getExportTemplate()
	 */
	@Override
	public String getExportTemplate() throws NotImplementedMethodException {
		return "appTemplates/patientLogTemplate.html";
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.giovanolli.mumi.controller.AbstractController#getModel()
	 */
	@Override
	protected AbstractModel<PatientLog> getModel() {
		return model;

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.giovanolli.mumi.controller.AbstractController#getService()
	 */
	@Override
	protected Service<PatientLog> getService() {
		return service;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.giovanolli.mumi.controller.AbstractController#init()
	 */
	public void init() {
		super.init();
		model.setFilter(new PatientLogFilter());
		populateNonEntityPropertiesToModel();
	}

	/**
	 * OBSERVATION METHODS.
	 */

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.giovanolli.mumi.controller.AbstractController#populateNonEntityPropertiesToModel()
	 */
	@Override
	public void populateNonEntityPropertiesToModel() {
		model.setSelectablePatients(patientService.getList(new PatientFilter()));
		model.setSelectableSpecialists(specialistService.getList(new SpecialistFilter()));
	}

	/**
	 * Prepare entity render.
	 * 
	 * @param uid
	 *            the uid
	 * @param useCaseMode
	 *            the use case mode
	 * @param patient
	 *            the patient
	 * @throws InstantiationException
	 *             the instantiation exception
	 * @throws IllegalAccessException
	 *             the illegal access exception
	 */
	public void prepareEntityRender(String uid, UseCaseMode useCaseMode, Patient patient) throws InstantiationException, IllegalAccessException {
		super.prepareEntityRender(uid, useCaseMode);
		if (patient != null) {
			model.setPatient(patient);
		}
	}
}
